package com.lagou.M3Homework;

public class ToStringTest {
    public static void main(String[] args) {
        int[] arr1 = null;
        System.out.println(toString(arr1));
        int[] arr2 = new int[0];
        System.out.println(toString(arr2));
        int[] arr3 = {1, 2, 3, 4, 5};
        System.out.println(toString(arr3));
    }



    public static String toString(int[] arr) {
        if (arr == null) {
            return "null";
        }
//        int maxLen = arr.length-1;
        if (arr.length-1 == -1)
            return "[]";

        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (int i = 0; i < arr.length; i++) {
            sb.append(arr[i]);
            if (i == arr.length-1){
                return sb.append(']').toString();
            }
            sb.append(",");
        }
        return sb.toString();
    }
}
